package com.xq.shop.service.user.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xq.shop.dao.IPointDAO;
import com.xq.shop.dao.IPointRecordDAO;
import com.xq.shop.dao.IUserDAO;
import com.xq.shop.po.Point;
import com.xq.shop.po.PointRecord;
import com.xq.shop.po.User;
import com.xq.shop.service.user.IPointService;
import com.xq.shop.util.DateUtil;
import com.xq.shop.vo.PageInfo;
import com.xq.shop.vo.SearchObject;

@Service("user_point_service")
public class PointService extends BaseService<Point> implements IPointService {

	@Autowired
	private IPointDAO pointDAO;
	
	@Autowired
	private IPointRecordDAO recordDAO;
	
	@Autowired
	private IUserDAO userDAO;
	
	@Override
	public IPointDAO getDAO() {
		return pointDAO;
	}

	/**
	 * 积分新增
	 * 根据积分处理类型的不同,将积分新增划分成3钟积分操作,1获取,2系统赠送,3支付使用
	 * 1获取,获取是用户通过下订单，订单完成后系统根据订单金额的10%的数值新增积分
	 * 2系统赠送,后台管理员可在系统管理界面对指定用户进行积分赠送
	 * 3支付使用,用户用积分进行支付使用时，判断用户未过期积分是否够支付，如果不够支付，提示用户余额不足。
	 * 如果积分充足，使用最早未过期积分支付，支付完毕更改积分状态。
	 */
	public void addPoint(Long userId, int p, int type, String orderSN, String detail) throws Exception{
		
		if(userId == null || p < 0) return;
		
		switch (type) {
			case PointRecord.GRANT:
			case PointRecord.SYSTEM_GRANT:
				
				Point point = pointDAO.get(userId, new Date());
				if(point == null){
					point = new Point();
					point.setUser_id(userId);
					point.setStatus(Point.NEW);
					point.setYear(DateUtil.format(new Date(), DateUtil.FORMAT_05));
				}
				
				point.setPoint(point.getPoint() + p);
				point.setRemain(point.getRemain() + p);
				if(point.getStatus() == Point.OVER){
					point.setStatus(Point.NEW);
				}
				pointDAO.saveOrUpdate(point);
			break;
			
			case PointRecord.PAY:
				
				List<Point> point_List = pointDAO.get(userId, Point.NEW);
				if(point_List == null || point_List.size() < 1){
					throw new Exception("用户积分不足");
				}
				
				int remain = 0, index = point_List.size() - 1;
				for(; index>=0; index--){
					
					remain += point_List.get(index).getRemain();
					if(index == 0 && remain < p){
						throw new Exception("用户积分不足");
					} if(index == 0 || remain >= p){
						break;
					}
				}
				
				if(p == remain){
					point_List.get(index).setStatus(Point.OVER);
				}
				point_List.get(index++).setRemain(remain - p);
				
				for(; index<point_List.size(); index++){
					
					point_List.get(index).setRemain(0);
					point_List.get(index).setStatus(Point.OVER);
				}
				
				pointDAO.update(point_List);
			break;
			default:
				break;
		}
		
		List<Point> point_List = pointDAO.get(userId, Point.NEW);
		int remain = 0;
		for(Point point : point_List){
			remain += point.getRemain();
		}
		User user = userDAO.get(userId);
		user.setPoint(remain);
		userDAO.update(user);
		
		PointRecord record = new PointRecord();
		record.setUser_id(userId);
		record.setType(type);
		record.setPoint(p);
		record.setOrder_sn(orderSN);
		record.setDetail(detail);
		recordDAO.save(record);
	}

	@Override
	public PageInfo<PointRecord> getRecord(SearchObject search) {
			
		long total = 0;
		List<PointRecord> result = new ArrayList<PointRecord>();
		total = recordDAO.getCount(search);
		result = recordDAO.get(search);
		
		return new PageInfo<PointRecord>(search, result, total);
	}
}
